package com.neo.base.function

import java.util.Date

//函数值
class FunctionValueTest {
  def totalResuleOverRange(number: Int , codeBlock: Int => Int)  : Int = {
    var result   = 0
    for(i <- 1 to number){
      result += codeBlock(i)
    }
    result
  }

  def inject(arr: Array[Int] , initial: Int , operation: (Int,Int) => Int): Int = {
    var carryOver = initial
    arr.foreach(element => carryOver = operation(carryOver,element))
    carryOver
  }



  def inject1(arr: Array[Int] , initial: Int , operation: (Int,Int) => Int): Int = {
    var carryOver = initial
    arr.foreach(element => carryOver = operation(carryOver,element))
    println(carryOver)
    carryOver

  }
  //函数curry化

  def inject2(arr: Array[Int], initial: Int)(operation:(Int,Int) => Int): Int = {
    var carryOver = initial
    arr.foreach(element => carryOver = operation(carryOver,element))
    println(carryOver)
    carryOver
  }

  //参数位置标记


  //偏函数

  def printValueApp(data: Date, message: String): Unit ={
    println(data + "++++++++++++++++" + message)
  }



  //闭包

  def loopThrough(number: Int)(closure: Int => Unit) {
    for (i <- 1 to number) { closure(i) }
  }



}


object FunctionValueTest{
  def main(args: Array[String]): Unit = {
    val valeTest = new FunctionValueTest
    println(valeTest.totalResuleOverRange(11,i => if(i%2 != 0) 1 else 0))
    println(valeTest.totalResuleOverRange(11,i => if(i%2 == 0) 1 else 0))
    val aa = Array(1,2,3,6,10)

    def opppp (carryOver: Int, element: Int): Int ={
      println("carryOver=",carryOver,"   element=",element)
      carryOver + element
    }
    def maaax (carryOver: Int, element: Int): Int ={
      println("carryOver=",carryOver,"   element=",element)
      Math.max(carryOver,element)
    }

    valeTest.inject1(aa,0,opppp) //求和
    valeTest.inject1(aa,0,maaax) //求最大值
    valeTest.inject2(aa,0)(maaax)

    val a = Array("Hello","world")
    val b= Array("Hello","world")


    a.corresponds(b)(_.equalsIgnoreCase(_))


    println("---------------参数位置标记------------------------------")
    val arr = Array(1,2,3,4)
    println("Sum of all values in Array is " + (0 /: (1 to 10)){ (sum, elem) => sum + elem})

    println("-------------偏函数----------------------------------------------")

    valeTest.printValueApp(new Date(),"asdfasdf")


    val printValueAppP = valeTest.printValueApp(new Date(),_: String)

    printValueAppP("aaaaaaa")
    printValueAppP("bbbbbbbbbbbbbbbbbbbbb")

    println("--------------------闭包--------------------------")

    def aaa(x: Int): Unit = {
      println(x)
    }
    valeTest.loopThrough(5)(aaa)
    var result = 0
    val addIt = {value: Int => result += value}
    valeTest.loopThrough(10)(addIt)
    println(result)

  }
}


